/*
 * Created by Federico Pisarello - SisDev Software (c) 2012 - fpisarello@sisdevsoft.com
 */

var popForm = 'phrasesPop.php';
var actionForm = 'phrasesAction.php';

var joDataPhrase;
var oTable;

/** Variable of tracking the IndexPK **/
var actionPK = undefined;
var action = undefined;

function $_init()
{	
	var gaiSelected;
	
	$(document).ready(function() {
    	oTable = $('#dt_phrases').dataTable( {
					"bJQueryUI": true,
					"sPaginationType": "two_button",
					"bInfo": false,
					"bLengthChange": false,
					"bProcessing": true,
					"bServerSide": true,
					"sAjaxSource": actionForm + "?actionOfForm=search",
					"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
						if ( jQuery.inArray(aData[0], gaiSelected) != -1 )
						{
					        $(nRow).addClass('row_selected');
						}
						return nRow;
					},
					"aoColumns": [
					  			null,
					  			null,
					  			null
					  		]

				});
    	
    	/* Click event handler */
    	$('#dt_phrases tbody tr').live('click', function () {
    		var aData = oTable.fnGetData(this);
    		var iId = aData[0];
    		actionPK = aData[0];
    		
    		gaiSelected =  [];
    		if ( $(this).hasClass('row_selected') ) {
	            $(this).removeClass('row_selected');
	            actionPK = undefined;
	        }
	        else {
	            oTable.$('tr.row_selected').removeClass('row_selected');
	            $(this).addClass('row_selected');
	            gaiSelected[gaiSelected.length++] = iId;
	        }
    	} );

	});
	
	$("#phrasesDialog").dialog({
		autoOpen:false, 
		height:'auto', 
		width:600, 
		modal: true,
		resizable: false,
		closeOnEscape: false,
		position : ['center',2],		
		dialogClass:'no-close',
		beforeClose: function() { $_ClearPhrasesPopForm(); },
	});
	
	//Add Click for All Buttons in Search Form
	$("#btnAdd").click( function(e) {
		addProxyShow();
    });
		
	$('#btnEdit').click( function(e) {
		editProxyShow();
	});
}

function $_initPop()
{	
	var idTypeUser;
	
	//Add Click for All Buttons in Pop Form
	$('#btnSave').click( function(e) {
		$('#phrasesPopForm').submit();
	});
	$('#btnExit').click( function(e) {
		$('#phrasesDialog').dialog('close');
	});
	
	//Populate User Data	
	if(action == iConst.EDIT)
	{
		$('#description').val(joDataPhrase.description);
		$('#cmbLanguage').val(joDataPhrase.language);
	}
			
	$_ValidatorSetDefaults();
	$_initPopValidator();	
}

function addProxyShow()
{
	//Reset State of action and IndexPK
	actionPK = undefined;
	action = iConst.NEW;
	oTable.$('tr.row_selected').removeClass('row_selected');
	$('#phrasesDialog').load(popForm).dialog('open');
}

function editProxyShow()
{
	if(actionPK === undefined)
	{
		$_showMessage('ALERT', 'ALERTA', strMenssageSelectionRecordInGrid);
		return;
	}
	
	action = iConst.EDIT;
	var paramData =  "actionOfForm=" + iConst.EDIT + "&idPhrase=" + actionPK;
	
    $.ajax({
        type: 'POST',
        url: actionForm,
        data: paramData,
		dataType: 'json',
        success: function(jsonObj)
                            {
        						joDataPhrase = jsonObj;
        						//Open Pop Dialog
        						$('#phrasesDialog').load(popForm).dialog('open');
                            }
        });
	
return true;
}

function $_savePhrase(action)
{
	var paramData = 'actionOfForm=addOrEdit&' + $("#phrasesPopForm").serialize();
	
	if(action == iConst.EDIT)
		paramData += '&idPhrase=' + actionPK;
	
    $.ajax({
        type: 'POST',
        url: actionForm,
        data: paramData,
		dataType: 'json',
        success: function(jsonObj){
            if(jsonObj.status === 'OK'){
				$_showMessage('OK', jsonObj.status, jsonObj.msg);
				$('#phrasesDialog').dialog('close');
				oTable.fnReloadAjax();
			}
			else
				$_showMessage('ERR', jsonObj.status, jsonObj.msg);
        }
    });

return true;
}

function $_initPopValidator()
{
	$('#phrasesPopForm').validate({
		submitHandler: function() {
			$_savePhrase(action);
		}, 		
		rules: {
			description: {required: true,minlength: 1},
			cmbLanguage: {required: true,minlength: 1}
		},
		messages: {
			description: {required: '',minlength: ''},
			cmbLanguage: {required: '',minlength: ''}
		}
	});
}

function $_ClearPhrasesPopForm()
{	
	$("#phrasesDialog").empty();
}